import 'dart:io';

import 'package:path/path.dart' as p;

import 'package:audioplayers/audioplayers.dart';
import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/bean/response/GetMusicListByTagResponse.dart';
import 'package:flutter_my_web/bean/response/get_music_tag_response.dart';
import 'package:flutter_my_web/constants/constant_status.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:get/get.dart';

class MusicController extends GetMaterialController{
  static List<GetMusicTagBean> tags = [];
  AudioPlayer audioPlugin = AudioPlayer();
  GetMusicListByTagResponseData? curPlayMusicBean;//当前播放的音乐
  var _music_list_map = Map<String,GetMusicListByTagResponse>();
  List<GetMusicListByTagResponseData> _musicList = [];
  var curMusicAllDuration = 0;//音乐总时长
  var curMusicProgress = 0.0.obs;
  var curMusicTag = "";
  @override
  Future<void> onInit() async {
    super.onInit();
    getMusicTag();
    audioPlugin.onPlayerStateChanged.listen((event) {
      if(event == PlayerState.playing){
      }

      refresh();
    });
    audioPlugin.onPositionChanged.listen((event) {
      curMusicProgress.value = event.inSeconds/curMusicAllDuration;
    });
  }

  Future<void> getMusicTag() async {
    // if(tags.isEmpty){
    //   var response =await FileManagerApi().getMusicTag();
    //   tags = response.data;
    //   tags.forEach((element) {
    //     print(element.name);
    //   });
    // }
    LogUtils.e("getMusicTag:${tags.length}");
  }


  Future<GetMusicListByTagResponse?>getMusicListByTag(String tag)async{
    // var bean = _music_list_map[tag];
    // if(bean==null){
    //   var response = await FileManagerApi().getMusicListByTag(tag);
    //   _music_list_map[tag] = response;
    //
    //   _musicList = [];
    //   _music_list_map.keys.forEach((element) {
    //     GetMusicListByTagResponse? response = _music_list_map[element];
    //     _musicList.addAll(response!.data);
    //   });
    //
    //   return response;
    // }else{
    //   return bean;
    // }

  }


  bool isHavePlay(){
    if(curPlayMusicBean==null||curPlayMusicBean!.playStatusEnum==PlayStatusEnum.stop){
      return false;
    }
    return true;
  }
  Future<void> downCurMusic() async {
    if(curPlayMusicBean == null){
      showSnackBar("当前无音乐");
      return;
    }
    if (GetPlatform.isWeb){
      showSnackBar("web 暂不支持保存");
      return;
    }
    var directoryPath = await FilePicker.platform.getDirectoryPath();
    LogUtils.e("directoryPath:${directoryPath}");
    if (directoryPath == null) {
      showSnackBar("没有选择文件夹");
      return;
    }
    var str = p.join(directoryPath, "${curPlayMusicBean!.name}.${curPlayMusicBean!.type}");
    print("保存音乐地址：${str}");
    var response = await Dio().download(curPlayMusicBean!.netPath!,str);
    print("保存音乐：${response.statusCode}");
    showSnackBar("保存音乐：${response.statusCode}");
  }
  Future<void> playCurMusic() async {
    if(_musicList.isEmpty){
      return;
    }
    if(curPlayMusicBean!=null){
      if(curPlayMusicBean!.playStatusEnum == PlayStatusEnum.play_ing){
        curPlayMusicBean!.playStatusEnum = PlayStatusEnum.stop;
        audioPlugin.pause();
      }else{
        curPlayMusicBean!.playStatusEnum = PlayStatusEnum.play_ing;
        audioPlugin.resume();
      }
    }else{
      await nextMusic();
    }

    refresh();
  }
  //下一首
  Future<void>nextMusic()async{
    if(_musicList.isEmpty){
      showSnackBar("暂无音乐");
      return;
    }
    if(curPlayMusicBean==null){
      curPlayMusicBean = _musicList.first;
    }else{
      int index = 0;
      for(var i=0;i<_musicList.length;i++){
        var element = _musicList[i];
        if(element.id == curPlayMusicBean!.id){
          index = i;
        }
      }
      if(index==_musicList.length-1){
        index = 0;
      }else{
        index = index+1;
      }
      curPlayMusicBean = _musicList[index];
    }
    await audioPlugin.play(UrlSource(curPlayMusicBean!.netPath!.replaceAll("http:", "https:")));
    refresh();

  }
  //上一首
  Future<void>previousMusic()async{
    if(_musicList.isEmpty){
      showSnackBar("暂无音乐");
      return;
    }
    if(curPlayMusicBean==null){
      curPlayMusicBean = _musicList.first;
    }else{
      int index = 0;
      for(var i=0;i<_musicList.length;i++){
        var element = _musicList[i];
        if(element.id == curPlayMusicBean!.id){
          index = i;
        }
      }
      if(index==0){
        index = 0;
      }else{
        index = index-1;
      }
      curPlayMusicBean = _musicList[index];
    }
    await audioPlugin.play(UrlSource(curPlayMusicBean!.netPath!.replaceAll("http:", "https:")));
    refresh();
  }


}